package com.mf.entity.vo.dept;

import com.mf.entity.bo.PositionBo;
import io.github.heollhai.common.utils.VerifyUtils;
import io.github.heollhai.common.utils.tree.AbstractTreeNode;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.BeanUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: lmf
 * @Create: 2024/9/20 13:14
 * Description: 部门岗位集合
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class DeptAndPositionVo extends AbstractTreeNode<DeptAndPositionVo,Integer,Integer> {
    @Schema(description = "部门id")
    private Integer id;
    @Schema(description = "部门名称")
    private String name;
    @Schema(description = "所属上级")
    private Integer pid;

    @Schema(description = "1部门，2：岗位")
    private Integer type = 1;
    @Schema(description = "管理Id(部门：负责人id,岗位所属部门id)")
    private Integer managerId;

    @Schema(description = "1启用，0禁用")
    private Integer status;
    @Schema(description = "备注")
    private String remark;
    @Schema(description = "排序")
    private Integer sort;


    private LocalDateTime createdTime;

    private LocalDateTime updateTime;

    /**
     * 设置岗位集合
     */
    public void setPositionList (List<PositionVo> positionList){
        if (VerifyUtils.isEmpty(positionList)) {
            return;
        }
        List<DeptAndPositionVo> deptAndPositionVos = convert(positionList);
        deptAndPositionVos.addAll(getChildren());
        setChildren(deptAndPositionVos);
    }
    /**
     * 递归将岗位集合Vo 装换为 deptAndPositionVo
     */
    public static List<DeptAndPositionVo> convert(List<PositionVo> positionList){
        List<DeptAndPositionVo> deptAndPositionVos = new ArrayList<>();
        for (PositionVo positionVo : positionList) {
            DeptAndPositionVo deptAndPositionVo = new DeptAndPositionVo();
            BeanUtils.copyProperties(positionVo,deptAndPositionVo);
            deptAndPositionVo.setType(2);
            deptAndPositionVo.setManagerId(positionVo.getDeptId());
            if(VerifyUtils.isNotEmpty(positionVo.getChildren())){
                deptAndPositionVo.setChildren(convert(positionVo.getChildren()));
            }
            deptAndPositionVos.add(deptAndPositionVo);
        }
        return deptAndPositionVos;
    }

}
